package com.highper.common.exception;

import com.highper.common.constant.NumberConstant;
import com.highper.common.constant.ResponseCodeConstant;
import com.highper.common.constant.StringConstant;
import com.highper.common.entity.exception.HighperFeignException;
import com.highper.common.entity.response.HighperResponse;
import com.highper.common.util.ServletUtil;
import com.highper.common.util.WebFluxUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.WebExchangeBindException;

import java.util.List;

/**
 * @Author ZhangGJ
 * @Date 2022/01/26 10:09
 */
@Slf4j
public class BaseExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public HighperResponse handleException(Exception e) {
        String message = e.getMessage();
        log.error(message, e);
        ServletUtil.makeExceptionResponse(MediaType.APPLICATION_JSON_VALUE,
                ResponseCodeConstant.FAILURE);
        return HighperResponse.failure(ResponseCodeConstant.FAILURE, message);
    }

    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public HighperResponse handleRuntimeException(Exception e) {
        String message = e.getMessage();
        log.error(message, e);
        ServletUtil.makeExceptionResponse(MediaType.APPLICATION_JSON_VALUE,
                ResponseCodeConstant.FAILURE);
        return HighperResponse.failure(ResponseCodeConstant.FAILURE, message);
    }

    @ResponseBody
    @ExceptionHandler(value = WebExchangeBindException.class)
    public HighperResponse handleWebExchangeBindException(WebExchangeBindException e) {
        StringBuilder message = new StringBuilder();
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        for (FieldError error : fieldErrors) {
            message.append(error.getField()).append(StringConstant.SPLIT_SPACE).append(error.getDefaultMessage())
                    .append(StringConstant.SPLIT_ENGLISH_COMMA);
        }
        message = new StringBuilder(
                message.substring(NumberConstant.ZERO, message.length() - NumberConstant.ONE));
        log.error(message.toString(), e);
        ServletUtil.makeExceptionResponse(MediaType.APPLICATION_JSON_VALUE,
                ResponseCodeConstant.BAD_REQUEST);
        return HighperResponse.failure(ResponseCodeConstant.BAD_REQUEST, message.toString());
    }

    @ResponseBody
    @ExceptionHandler(value = HighperFeignException.class)
    public HighperResponse handleHighperFeignException(Exception e) {
        String message = e.getMessage();
        log.error(message, e);
        ServletUtil.makeExceptionResponse(MediaType.APPLICATION_JSON_VALUE,
                ResponseCodeConstant.FAILURE);
        return HighperResponse.failure(ResponseCodeConstant.FAILURE, message);
    }
}
